<template>
  <div>
    <el-input placeholder="输入关键字进行过滤" v-model="filterText"> </el-input>
    <el-tree
      ref="tree"
      :filter-node-method="filterNode"
      lazy
      :load="loadNode"
      :props="props"
      node-key="id"
      class="treeStyle"
    >
    </el-tree>
  </div>
</template>

<script>
import jsonData from '@/assets/queryByEquiDesc.json'
export default {
  name: 'LazyTree',
  data () {
    return {
      treeData: jsonData,
      props: {
        label: (item) => {
          return `${item.equiDesc}（${item.floc}）`
        },
        id: 'floc'
      },
      filterText: ''
    }
  },
  created () {
    console.log(this.treeData)
  },
  watch: {
    filterText (val) {
      this.$refs.tree.filter(val)
    }
  },
  methods: {
    filterNode (value, data) {
      if (!value) return true
      return data.equiDesc.indexOf(value) !== -1
    },
    loadNode (node, resolve) {
      console.log(node)
      // 如果展开第一级节点，从后台加载一级节点列表
      if (node.level === 0) {
        const data = this.treeData.data.dataList.find(
          (item) => item.floc === '2521'
        )
        console.log(data)
        return resolve([data])
      } else {
        const data = this.treeData.data.dataList.filter((item) => {
          return item.lastKks === node.data.floc
        })
        console.log(data)
        return resolve(data)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.treeStyle{
  height: 60vh;
  overflow: auto;
}
</style>
